import { Injectable } from '@angular/core';
import { AppConfig } from "../config";
import { MatrixLogicGroup } from "../entity/matrix-logic-group.model";
import { Http,Headers } from "@angular/http";
import { Observable } from "rxjs/Observable";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class MatrixLogicGroupService {
  private readonly param = "matrixLogicGroups";
  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }


  constructor(private http: InterceptorService) { }

  getMatrixLogicGroups(): Promise<MatrixLogicGroup[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.json().data as MatrixLogicGroup[])
      .catch(this.handleError);
  }
  /**
   * 根据状态分组ID获得逻辑组列表
   * 
   * @param {number} stateGroupid 
   * @returns {Promise<MatrixState[]>} 
   * @memberof MatrixStateService
   */
  getMatrixLogicGroupsByStateGroupId(stateGroupId:number): Promise<MatrixLogicGroup[]> {
    return this.http.get(`${this.url}/stateGroupId/${stateGroupId}`) //(`http://192.168.15.18:8585/webapi/v1/matrixLogicGroups/stateGroupId/${stateGroupId}`)  
      .toPromise()
      .then(response => response.json().data as MatrixLogicGroup[])
      .catch(this.handleError);
  }

  create(matrixLogicGroup: MatrixLogicGroup): Promise<MatrixLogicGroup> {
    return this.http.post(this.url, JSON.stringify(matrixLogicGroup))
      .toPromise()
      .then(res => {
        try {
          let result = res.json();
          switch (result.code) {
            case "200":
              return result.data as MatrixLogicGroup;
            case "-1":
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }
  update(matrixLogicGroup: MatrixLogicGroup): Promise<MatrixLogicGroup> {
    const url = `${this.url}`;
    return this.http.put(url, JSON.stringify(matrixLogicGroup))
      .toPromise()
      .then(() => matrixLogicGroup)
      .catch(this.handleError);
  }

  //根据Name判断是否已经存在
  hasLogicGroupName(logicGroupName: string, logicGroupId: number, stateGroupId:number): Observable<boolean> {
    const url = `${this.url}/logicGroupName/${logicGroupName}/logicGroupId/${logicGroupId}/stateGroupId/${stateGroupId}`;
    return this.http.get(url).map(response => {
      let code = response.json().code;
      switch (code) {
        case "3001":
          return true;
        case "3002":
          return false;
      }
    }).catch(this.handleError);
  }

  //删除
  delete(id: number): Promise<void> {
    const url = `${this.url}/${id}`;
    return this.http.delete(url)
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }
  //批量线路删除
  batchDelete(keyIds: number[]): Promise<boolean> {
    const apiUrl = `${this.url}?items=${keyIds}`;
    return this.http.delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response.json();
          switch (result.code) {
            case "200":
              return true;
            case "-1":
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    // console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }

}



// WEBPACK FOOTER //
// ./src/app/service/matrix-logic-group.service.ts